package com.hesicare.adapter.service.impl;

import com.hesicare.adapter.service.IParseBedService;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import static com.hesicare.untils.exceptionUtils.exceptionutil.errorTrackSpace;

/**
 * @author 55097
 */
@Service
public class ParseBedServiceImpl implements IParseBedService {

    protected final static Logger LOGGER = LoggerFactory.getLogger(ParseBedServiceImpl.class);


    public static Map<String, String> bedMap = new HashMap<>();

    @Override
    public void parseBed(Object obList) {
        try {
            Document doc = DocumentHelper.parseText(obList.toString());
            Element rootElt = doc.getRootElement();
            Element MsgInfo = rootElt.element("MsgInfo");
            Iterator iter = MsgInfo.elementIterator("Msg");
            while (iter.hasNext()) {
                Element recordEle = (Element) iter.next();
                Element msg = DocumentHelper.parseText(recordEle.getText()).getRootElement().element("body").element("row");
                String bedIndexNo = msg.elementText("BED_INDEX_NO");
                String bedCode = msg.elementText("BED_CODE");

                if (bedCode.length() >= 6) {
                    bedCode = bedCode.substring(3);
                }
                bedMap.put(bedIndexNo, bedCode);
            }
        } catch (DocumentException e) {
            LOGGER.error("解析床位xml错误", errorTrackSpace(e));
        } catch (NullPointerException e) {

        }
    }
}
